package com.spark.data

import android.annotation.SuppressLint
import android.app.Activity
import android.content.Intent
import android.os.Bundle
import android.support.v7.widget.LinearLayoutManager
import android.text.format.DateUtils
import android.view.View
import com.google.gson.Gson
import com.spark.colliery.common.ext.gone
import com.spark.common.BaseActivity
import com.spark.common.ext.sdf
import com.spark.data.model.BorrowListModel
import kotlinx.android.synthetic.main.frag_apply_info.*

class ApplyInfoActivity : BaseActivity() {
    private var borrowItemModel: BorrowListModel.ResultMap? = null

    companion object {
        fun actionStart(host: Activity, itemStr: String) {
            val intent = Intent(host, ApplyInfoActivity::class.java)
            intent.putExtra("borrowItem", itemStr)
            host.startActivity(intent)
        }
    }

    override fun contentResId(): Int {
        return R.layout.frag_apply_info
    }

    @SuppressLint("SetTextI18n")
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        borrowItemModel = intent?.getStringExtra("borrowItem")?.let { Gson().fromJson(it, BorrowListModel.ResultMap::class.java) }
        iv_back.setOnClickListener { finish() }

        borrowItemModel?.apply {
            tv_state.text = this.taskStateFlag
            tv_data_name.text = this.name
            tv_serial.text = "编号: ${this.number}"
            tv_regular.text = "规范: ${this.stdname}"
            tv_create_time.text = "创建: ${sdf().format(this.editionTime)}"
            tv_tags.text = "标签: ${this.keysName}"
            recy_books.layoutManager = object : LinearLayoutManager(this@ApplyInfoActivity) {
                override fun canScrollVertically(): Boolean {
                    return false
                }
            }
            val borrowBookAdapter = BorrowBookAdapter(R.layout.item_borrow_book, false)
            recy_books.adapter = borrowBookAdapter
            borrowBookAdapter.addData(this.fileList)

            tv_days.text = DateUtils.getRelativeTimeSpanString(this@ApplyInfoActivity, this.editionTime - System.currentTimeMillis())
            tv_create.text = sdf().format(this.editionTime)
            et_reason.setText("后台要加字段")

            iv_arr.setOnClickListener {
                val visible = ll_info.visibility == View.VISIBLE
                ll_info.gone(visible)
                iv_arr.setImageResource(if (visible)
                    R.drawable.ic_arrow_down_blue
                else
                    R.drawable.ic_arrow_up_blue
                )
            }
        }
    }
}